package com.cslg.ac.demo.controller.host;
import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.entity.host.Host;
import com.cslg.ac.demo.entity.host.HostFoster;
import com.cslg.ac.demo.entity.user.User;
import com.cslg.ac.demo.service.host.HostService;
import com.cslg.ac.demo.util.communicate.ResponseUtil;
import com.cslg.ac.demo.util.integer.IntegerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@Controller
@PropertySource(value = {"classpath:host.properties"},encoding="utf-8")
public class HostController {
    @Autowired
    HostService hostService;
    @Value("${nameLength}")
    int nameLength;
    @Value("${address}")
    int addressLength;
    @Value("${phoneLength}")
    int phoneLength;
    @Value("${textLength}")
    int textLength;
    @Value("${petTypeLength}")
    int petTypeLength;




    @RequestMapping(value = "/host/sort")
    public void sortByCredit(@RequestParam(required = false)Double latitude, @RequestParam(required = false)Double longitude,
                             @RequestParam("offset")int offset, @RequestParam("num")int num,
                             @RequestParam(required = false)String address, @RequestParam(required = false)Integer credit,
                             PrintWriter out){
        Integer[] ids;
        boolean flag = (offset < 1 || num < 0);
        if(flag){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        try {
            ids = hostService.sortHost(latitude, longitude, offset - 1, num, address, credit);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
            return;
        }
        Map<String, Object> param = new HashMap<>(1);
        if(ids == null || ids.length < 1){
            ids = null;
        }
        param.put("ids", ids);
        ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, param);
    }



    @RequestMapping(value = "/host/getHostFosterById")
    public void getHostFosterById(@RequestParam int fosterId, HttpSession session, PrintWriter out){
        User user = (User)session.getAttribute("user");
        HostFoster hostFoster;
        try {
            hostFoster = hostService.getHostFosterById(fosterId, user.getId());
        }catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        Map<String,Object> param = new HashMap<>();
        param.put("hostFoster", hostFoster);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);
    }

    @RequestMapping(value = "/host/updateState")
    public void updateState(@RequestParam int fosterId, @RequestParam int state,
                            HttpSession session, PrintWriter out){
        User user = (User)session.getAttribute("user");
        int code;
        try {
            code = hostService.updateState(fosterId, user.getId(), state);
        }catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
            return;
        }
        ResponseUtil.responseCode(out,code);

    }


    @RequestMapping(value = "/host/updateInfo", method = RequestMethod.POST)
    public void updateTime(@RequestParam String beginTime, @RequestParam String endTime,
                           @RequestParam int fosterId,@RequestParam(required = false, defaultValue = "0")int newFosterId,
                           HttpSession session, PrintWriter out){
        User user = (User)session.getAttribute("user");
        HostFoster hostFoster = new HostFoster();
        hostFoster.setBeginTime(beginTime);
        hostFoster.setEndTime(endTime);
        hostFoster.setFosterId(fosterId);
        if(!hostFoster.valid()){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        int code;
        try {
            code = hostService.updateInfo(beginTime, endTime, fosterId, user.getId(), newFosterId);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        ResponseUtil.responseCode(out,code);


    }

    @RequestMapping(value = "/host/fostersPet", method = RequestMethod.POST)
    public void fostersPet(HostFoster hostFoster, HttpSession session, PrintWriter out){
        User user = (User)session.getAttribute("user");
        if(!hostFoster.valid()){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        int code;
        try {
            code = hostService.fostersPet(hostFoster);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
            return;
        }
        ResponseUtil.responseCode(out,code);
    }

    @RequestMapping(value = "/host/getInfo",method = RequestMethod.POST)
    public void getInfo(@RequestParam int id,PrintWriter out){
        Host host;
        try {
            host = hostService.getInfo(id);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        Map<String,Object> param = new HashMap<>();
        param.put("host",host);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);
    }

    @RequestMapping(value = "/host/beHost",method = RequestMethod.POST)
    public void beHost(Host host, HttpSession session, PrintWriter out){
        User user = (User)session.getAttribute("user");
        if(!host.insertValid(addressLength,phoneLength,textLength,nameLength)){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        host.setId(user.getId());
        int code;
        try {
            code = hostService.beHost(host);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        ResponseUtil.responseCode(out,code);
    }
}
